package com.xcity.game.pet;

import org.slf4j.Logger;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.pet.template.PetSkillLevelUpTemplate;
import com.xcity.game.pet.template.PetSkillTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;

public class PetSkill {

	@JSONField(name = "1")
	protected int id;
	@JSONField(name = "2")
	protected int level;
	
	@JSONField(serialize = false)
	protected PetSkillTemplate template;
	
	protected Pet pet;
	
	protected static final Logger LOG = PetService.LOG;
	
	public PetSkill() {}
	
	public PetSkill(int id, Pet pet) {
		this.id = id;
		this.level = 1;
		this.pet = pet;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public PetSkillTemplate getTemplate() {
		return template != null ? template : (template = App.getApp().getServiceManager().get(TemplateService.class).get(PetSkillTemplate.class, id));
	}

	public void setTemplate(PetSkillTemplate template) {
		this.template = template;
	}
	
	@JSONField(serialize = false)
	public PetSkillLevelUpTemplate getLevelUpTemplate() {
		return getTemplate().getLevelUpTemplate(level);
	}
	
	@JSONField(serialize = false)
	public int getMaxLevel() {
		return getTemplate().getLevelUpTemplates().size();
	}
	
	public void addLevel(int val, String cause) {
		int maxLvl = getMaxLevel();
		if (level >= maxLvl) {
			return;
		}
		int oldLvl = level;
		level += val;
		if (level > maxLvl) {
			level = maxLvl;
		}
		Player player = pet.vm.getOwner();
		LOG.info("[PET SKILLLVLUP]ID[{}]PET[{}]PETTID[{}]SKILL[{}]OLDLVL[{}]LEVEL[{}]CAUSE[{}]", player.getId(), pet.getId(), pet.getTemplateId(), id, oldLvl, level, cause);
	}
	
}
